package movieGandalf.DB;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.HashMap;
import movieGandalf.dataObjects.Movie;

public class MoviesReader implements DataReader{
	
	/*public int read(HashMap<String,Object> dest, Encoder enc, Connection conn, InputStream in){
		String line = null;
		
		try {
			boolean isBegin = true;
			while ((line = MovieTools.getLine(in)) != null){
				if (isBegin){
					if ((line.length()<3 || !line.subSequence(0, 3).equals("===")))
						continue;
					else{
						isBegin = false;
						continue;
					}
				}
							
				int len = line.length();

				if (len>=5 && line.startsWith("-----"))
					break;
				
				if (len==0 || line.charAt(0)=='"')
					continue;
				
				String imdbName = MovieTools.getIMDBName(line);
				if (imdbName.equals(""))
					continue;
				
				String movieName = MovieTools.getMovieName(line);
				if (movieName.equals(""))
					continue;
				
				Short year = MovieTools.getYear(line);
				if (year==-1)
					continue;
				
				dest.put(imdbName, new Movie(null,movieName,imdbName,year));
			}
		}
		catch (Exception e){
			e.printStackTrace();
		}
		finally {
			try{
				in.close();
			}
			catch(Exception e){
				return 0;
			}
		}
		
		return 1;
	}*/
	
	public int read(Connection conn, InputStream in){
		String line = null;
		List<String> imdbNames = new LinkedList<String>();
		List<String> movieNames = new LinkedList<String>();
		List<Short> years = new LinkedList<Short>();
		
		try {
			boolean isBegin = true;
			while ((line = MovieTools.getLine(in)) != null){
				if (isBegin){
					if ((line.length()<3 || !line.subSequence(0, 3).equals("===")))
						continue;
					else{
						isBegin = false;
						continue;
					}
				}
							
				int len = line.length();

				if (len>=5 && line.startsWith("-----"))
					break;
				
				if (len==0 || line.charAt(0)=='"')
					continue;
				
				String imdbName = MovieTools.getIMDBName(line);
				if (imdbName.equals(""))
					continue;
				
				String movieName = MovieTools.getMovieName(line);
				if (movieName.equals(""))
					continue;
				
				Short year = MovieTools.getYear(line);
				if (year==-1)
					continue;
				
				movieNames.add(movieName);
				imdbNames.add(imdbName);
				years.add(year);
			}
			

			PreparedStatement stmt = conn.prepareStatement(new String("REPLACE INTO Movies (movieName,imdbName,year) VALUES (?,?,?)"));
			
			Iterator<String> movieNameIter = movieNames.iterator();
			Iterator<String> imdbNameIter = imdbNames.iterator();
			Iterator<Short> yearsIter = years.iterator();
			String movieName = null;
			String imdbName = null;
			Short year = 0;
			
			while (movieNameIter.hasNext()){
				movieName = movieNameIter.next();
				imdbName = imdbNameIter.next();
				year = yearsIter.next();
				stmt.setString(1, movieName);
				stmt.setString(2, imdbName);
				stmt.setShort(3, year);
				stmt.addBatch();
			}
			stmt.executeBatch();
			conn.commit();
			
			stmt.close();
		}
		
		catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
		catch (Exception e){
			e.printStackTrace();
		}
		finally {
			try{
				in.close();
			}
			catch(Exception e){
				return 0;
			}
		}
		
		return 1;
	}
	
}